package com.dl.test.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author DL
 * @create 2025-03-25 14:54
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {
    @GetMapping("/export-easyexcel/{size}")
    public void exportEasyExcel(HttpServletResponse response, @PathVariable(name = "size", required = false) Integer size) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("example", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");

        if (size == null || size <= 0){
            size = 10;
        }

        // 准备表头
        List<List<String>> head = new ArrayList<>();
        head.add(Collections.singletonList("ID"));
        head.add(Collections.singletonList("Name"));
        head.add(Collections.singletonList("Age"));
        head.add(Collections.singletonList("TEST4"));
        head.add(Collections.singletonList("TEST5"));
        head.add(Collections.singletonList("TEST6"));
        head.add(Collections.singletonList("TEST7"));
        head.add(Collections.singletonList("TEST8"));
        head.add(Collections.singletonList("TEST9"));
        head.add(Collections.singletonList("TEST10"));

        // 准备数据
        List<Map<Integer, String>> data = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            Map<Integer, String> row = new HashMap<>();
            row.put(0, "1");
            row.put(1, "Alice");
            row.put(2, "25");
            row.put(3, "25");
            row.put(4, "25");
            row.put(5, "25");
            row.put(6, "25");
            row.put(7, "25");
            row.put(8, "25");
            row.put(9, "25");
            row.put(10, "25");
            data.add(row);
        }

        // 使用 EasyExcel 写入 Excel 文件
        EasyExcel.write(response.getOutputStream())
                .head(head)
                .sheet("测试")
                .doWrite(data);
    }

    @PostMapping("/stream-import-excel-map")
    public String streamImportExcel(@RequestParam("file") MultipartFile file) throws IOException {
        // 使用 EasyExcel 流式读取 Excel 文件
        EasyExcel.read(file.getInputStream(), new ReadListener<Map<Integer, String>>() {
            @Override
            public void invoke(Map<Integer, String> data, AnalysisContext context) {
                // 每一行数据都会调用此方法
                System.out.println("读取到一行数据：" + data);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                // 所有数据解析完成后的回调
                System.out.println("所有数据已解析完成");
            }
        }).sheet().doRead();

        return "导入成功";
    }
}
